package webExperiment;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Panel;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextArea;

public class QuestionPanel extends Panel {
    /**
	 * 
	 */
    private static final long serialVersionUID = 1L; // v2
    private final Color BGCOLOR = new Color(255, 254, 218);
    // long questions
    private MCQuestion lq1 = new MCQuestion(MCQuestion.LONG, 7, BGCOLOR);
    private MCQuestion lq2 = new MCQuestion(MCQuestion.LONG, 7, BGCOLOR);
    private MCQuestion lq3 = new MCQuestion(MCQuestion.LONG, 7, BGCOLOR);
    private MCQuestion lq4 = new MCQuestion(MCQuestion.LONG, 7, BGCOLOR);
    private MCQuestion lq5 = new MCQuestion(MCQuestion.LONG, 7, BGCOLOR);

    private MCQuestion sq1 = new MCQuestion(MCQuestion.SHORT, 7, BGCOLOR);
    private MCQuestion sq2 = new MCQuestion(MCQuestion.SHORT, 7, BGCOLOR);
    private MCQuestion sq3 = new MCQuestion(MCQuestion.SHORT, 7, BGCOLOR);
    private MCQuestion sq4 = new MCQuestion(MCQuestion.SHORT, 7, BGCOLOR);
    private MCQuestion sq5 = new MCQuestion(MCQuestion.SHORT, 7, BGCOLOR);
    private MCQuestion sq6 = new MCQuestion(MCQuestion.SHORT, 7, BGCOLOR);
    private MCQuestion sq7 = new MCQuestion(MCQuestion.SHORT, 7, BGCOLOR);
    private MCQuestion sq8 = new MCQuestion(MCQuestion.SHORT, 7, BGCOLOR);
    private MCQuestion sq9 = new MCQuestion(MCQuestion.SHORT, 7, BGCOLOR);
    private MCQuestion sq10 = new MCQuestion(MCQuestion.SHORT, 7, BGCOLOR);

    private FreeQuestion fq1 = new FreeQuestion(BGCOLOR);
    private FreeQuestion fq2 = new FreeQuestion(BGCOLOR);
    private FreeQuestion fq3 = new FreeQuestion(BGCOLOR);
    private FreeQuestion fq4 = new FreeQuestion(BGCOLOR);
    private FreeQuestion fq5 = new FreeQuestion(BGCOLOR);

    public JButton okButton = new JButton();
    private JTextArea TextArea = new JTextArea();
    private JLabel label1 = new JLabel();;
    private JLabel label2 = new JLabel();;
    private short lastPanelType;

    public QuestionPanel() {
	this.setVisible(false);
	this.setSize(500, 500);
	this.setLocation(0, 0);

	// this.setBackground(Color.white);
	label1.setPreferredSize(new Dimension(100, 20));
	label2.setPreferredSize(new Dimension(100, 20));
	label1.setText("");
	label2.setText("");
	label2.setFont(new Font("Arial", Font.ITALIC + Font.BOLD, 12));
	okButton.setText(">>");
	okButton.setPreferredSize(new Dimension(160, 25));
    }

    public void addMCQuestions(String choiceLeft, String choiceRight,
	    String quest1, String quest2, String quest3, String quest4,
	    String quest5) {

	lastPanelType = MCQuestion.LONG;
	removeAll();
	this.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));

	add(lq1);
	add(lq2);
	add(lq3);
	add(lq4);
	add(lq5);

	add(label1);
	add(okButton);
	add(label2);

	lq1.writeLabels(quest1, choiceLeft, choiceRight, 0);
	lq2.writeLabels(quest2, choiceLeft, choiceRight, 0);
	lq3.writeLabels(quest3, choiceLeft, choiceRight, 0);
	lq4.writeLabels(quest4, choiceLeft, choiceRight, 0);
	lq5.writeLabels(quest5, choiceLeft, choiceRight, 0);

    }

    public void setInfoText(String txt) {
	label2.setText(txt);
    }

    public void addItemList(String question, String choiceLeft,
	    String choiceRight, String item1, String item2, String item3,
	    String item4, String item5, String item6, String item7,
	    String item8, String item9, String item10) {
	lastPanelType = MCQuestion.SHORT;
	this.removeAll();
	this.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));

	TextArea.setEditable(false);
	TextArea.setLineWrap(true);
	TextArea.setWrapStyleWord(true);

	TextArea.setFont(new Font("Arial", Font.BOLD, 12));
	TextArea.setBackground(BGCOLOR);
	TextArea.setPreferredSize(new Dimension(450, 50));
	// TextArea.setHorizontalAlignment(SwingConstants.CENTER);
	TextArea.setForeground(Color.BLACK);
	add(TextArea);
	TextArea.setText(question);

	add(sq1);
	add(sq2);
	add(sq3);
	add(sq4);
	add(sq5);
	add(sq6);
	add(sq7);
	add(sq8);
	add(sq9);
	add(sq10);
	// writeLabels

	add(label1);
	add(okButton);
	add(label2);

	sq1.writeLabels(item1, choiceLeft, choiceRight, 0);
	sq2.writeLabels(item2, choiceLeft, choiceRight, 0);
	sq3.writeLabels(item3, choiceLeft, choiceRight, 0);
	sq4.writeLabels(item4, choiceLeft, choiceRight, 0);
	sq5.writeLabels(item5, choiceLeft, choiceRight, 0);
	sq6.writeLabels(item6, choiceLeft, choiceRight, 0);
	sq7.writeLabels(item7, choiceLeft, choiceRight, 0);
	sq8.writeLabels(item8, choiceLeft, choiceRight, 0);
	sq9.writeLabels(item9, choiceLeft, choiceRight, 0);
	sq10.writeLabels(item10, choiceLeft, choiceRight, 0);
    }

    public void addFreeQuestions(String quest1, String quest2, String quest3,
	    String quest4, String quest5) {

	lastPanelType = 0;
	removeAll();
	this.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
	add(fq1);
	add(fq2);
	add(fq3);
	add(fq4);
	add(fq5);

	add(label1);
	add(okButton);
	add(label2);

	fq1.writeLabels(quest1);
	fq2.writeLabels(quest2);
	fq3.writeLabels(quest3);
	fq4.writeLabels(quest4);
	fq5.writeLabels(quest5);
    }

    public String getMCAnswerString() {
	String an = "";
	if (lastPanelType == MCQuestion.LONG) {
	    if (lq1.isVisible())
		an = an + lq1.getSelectedItem();
	    if (lq2.isVisible())
		an = an + lq2.getSelectedItem();
	    if (lq3.isVisible())
		an = an + lq3.getSelectedItem();
	    if (lq4.isVisible())
		an = an + lq4.getSelectedItem();
	    if (lq5.isVisible())
		an = an + lq5.getSelectedItem();
	} else if (lastPanelType == MCQuestion.SHORT) {
	    if (sq1.isVisible())
		an = an + sq1.getSelectedItem();
	    if (sq2.isVisible())
		an = an + sq2.getSelectedItem();
	    if (sq3.isVisible())
		an = an + sq3.getSelectedItem();
	    if (sq4.isVisible())
		an = an + sq4.getSelectedItem();
	    if (sq5.isVisible())
		an = an + sq5.getSelectedItem();
	    if (sq6.isVisible())
		an = an + sq6.getSelectedItem();
	    if (sq7.isVisible())
		an = an + sq7.getSelectedItem();
	    if (sq8.isVisible())
		an = an + sq8.getSelectedItem();
	    if (sq9.isVisible())
		an = an + sq9.getSelectedItem();
	    if (sq10.isVisible())
		an = an + sq10.getSelectedItem();

	}
	return an;
    }

    public String getFreeAnswerString() {
	final String separator = "||";
	String an = "";
	if (lastPanelType == 0) {
	    if ((fq1.isVisible()))
		an = an + fq1.getAnswer() + separator;
	    if ((fq2.isVisible()))
		an = an + fq2.getAnswer() + separator;
	    if ((fq3.isVisible()))
		an = an + fq3.getAnswer() + separator;
	    if ((fq4.isVisible()))
		an = an + fq4.getAnswer() + separator;
	    if ((fq5.isVisible()))
		an = an + fq5.getAnswer() + separator;

	}
	return an;
    }

}// class

